package com.java.IO.file_stream;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterAPI {
    public static void main(String[] args) throws IOException {
        String path = "File和IO流\\com\\java\\files\\char_stream.txt";
        
        // 创建对象：若父级路径不存在则抛出异常FileNotFoundException，若文件不存在则会创建。若文件已存在则清空文件内数据，但设置参数2为true后则不会清空
        FileWriter fileWriter1 = new FileWriter(path);
        FileWriter fileWriter2 = new FileWriter(new File(path));
        FileWriter fileWriter3 = new FileWriter(new File(path), true);
        
        // write：写出数据到本地文件，若参数为整数则会转为ASCII对应的字符
        fileWriter1.write(97); // a
        fileWriter1.write('9'); // 9
        fileWriter1.write('中'); // 中
        fileWriter1.write('中'); // 中
        fileWriter1.write("\r\n"); // 换行(Windows\r\n、Linux\n、Mac\r)
        fileWriter1.write(new char[]{'a', '哈', 'c', 'd'}); // a哈cd
        fileWriter1.write(new char[]{'a', '哈', 'c', 'd', 101, 102, 103}, 3, 2); // de
        fileWriter1.write("啊啊啊Hello"); // 啊啊啊Hello
        fileWriter1.write("啊啊啊Hello", 3, 2); // He
        
        // close：关闭流并释放资源
        fileWriter1.close();
        fileWriter2.close();
        fileWriter3.close();
    }
}
